<?php
/**
 * Created by PhpStorm.
 * User: haobin
 * Date: 2016/11/3
 * Time: 14:09
 * 问题列表页
 */
require('../conn.php');
require('../functions.php');
require('admin.php');
if ($_POST) {
    $ids = $_POST['ids'];
    $model = $_POST['model'];
    if ($model == "del") {
        $row = $mysql->execute("delete from advise where id in ({$ids})");
        echo $row ? 1 : $mysql->error();
        exit();
    }else{//执行查询
        $question_name=trim($_POST['question_name']);

        $question_name= strstr($question_name,"~") ? explode("~",$question_name) : $question_name;

        if(is_array($question_name)){//如果是口令查找
            $questions=$mysql->query("select * from advise where $question_name[0] like '%$question_name[1]%' ");

            $question_name=$question_name[1];
        }else{//如果是模糊查找

            $questions=$mysql->query("select * from advise where CONCAT(id,uid,question,time) LIKE '%$question_name%'");
        }
       $questions= edit_font($questions,$question_name);//关键字标亮

    }
} else {
    $row=$mysql->query("select count(*) as max_num from advise");
    $max_num=$row[0]['max_num'];//获取总数
    $count_page=ceil($max_num/10);//最大页数

    $num = 10;//每页显示多少
    $page_num = isset($_GET["page_num"]) ? $_GET["page_num"] : 0;//获取第几页
    if($page_num<0)$page_num=0;//超出第一页只显示第一页
    if($page_num>$count_page-1)$page_num=$count_page-1;//超出最后一页只显示最后一页
    $start = $page_num * $num;//从多少条开始
    $type = $_GET['type'];
	if($type == 1){
		$questions = $mysql->query("select * from `advise` where `isresolve` = 1 order by time desc limit $start,$num");
	}else{
		$questions = $mysql->query("select * from `advise` where `isresolve` = 0 limit $start,$num ");
	}
        
    if($question_name=trim($_GET['question_name'])){
        $questions= edit_font($questions,$question_name);//关键字标亮
    };


}

function edit_font($questions,$question_name){
    foreach($questions as $key=>$val){
        foreach($val as $k=>$v){
            if(strstr($v,$question_name)) {
                $questions[$key][$k] =str_replace($question_name,"<span style='color:red'>$question_name</span>",$questions[$key][$k]);
            }
        }
    }
    return $questions;
}
?>
<?php include('head.php') ?>

<script>
    $(function () {
        $("#focusAllQuestion").click(function () {
            $("[name='question_ids']").prop("checked", true);//全选
        })
        $("#moveAllQuestion").click(function () {
            $("[name='question_ids']").prop("checked",false);//取消全选
        })
        $("#fanQuestion").click(function () {
            $("[name='question_ids']").each(function () {//反选
                if ($(this).prop("checked")) {
                    $(this).prop("checked",false);
                }
                else {
                    $(this).prop("checked", true);
                }
            })
        })
        $("#delQuestionByIds").click(function () {//输出选中的值
            $.post("questionList.php", {ids: getIds(), model: "del"}, function (data) {
                if (data == 1) {
                    location.reload();
                } else {
                    alert("删除失败:" + data);
                }
            })
        })
        $("#noAnswer").click(function(){
        	window.location.href = "questionList.php";//未回答
        })
         $("#yesAnswer").click(function(){
        	window.location.href = "?page_num=<?= $page_num - 1; ?>&question_name=<?=$question_name?>&type=1";//已回答
        })
    })
    function getIds() {
        text = $("input:checkbox[name='question_ids']:checked").map(function (index, elem){
            return $(elem).val();
        }).get().join(',');
        return text;
    }
</script>
<div class="container-fluid">
    <div class="row-fluid">
        <?php include('left.php'); ?>
        <div class="span10">
            <h1 class="page-title">问题列表</h1>

            <div class="well" >
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <div class="well" style="overflow: hidden;">
                        	<form  action="questionList.php" class="bs-example bs-example-form" role="form" method="post">

							        <div class="">
							            <div class="col-md-6">
                                            <strong class="text-info">输入查询指令如:id~12 (等于标号为12的数据)<br>
                                            <em>编号:id　用户ID:uid　标题:question　时间:time </em>
                                            </strong>
							                <div class="input-group">
							                    <input type="text" value="<?=$_POST['question_name']?>" class="form-control"  name="question_name" placeholder="">
							                    <span class="input-group-btn">
							                        <button class="btn btn-default"  type="submit" >搜索</button>
							                    </span>
							                </div><!-- /input-group -->
							            </div><!-- /.col-lg-6 -->
							        </div><!-- /.row -->
							    </form>
							    <div>
							    	<p><span style="color: red;">忘记密码问题：</span>可以到个人中心的-设置-页面里的修改密码项目里进行修改或者直接在app登录主页下的忘记密码按钮进入修改密码页面</p>
							    	<p><span style="color: red;">签到问题：</span>新版本阅读视频，就会有积分，转发的话就是右转那个小勾勾转发。同样有积分，签到是首页的小红包点进去就有了。
 转发到微信按朋友阅读给积分，QQ分享只是每次分享只记一次。</p>
 									<p><span style="color: red;">系统升级：</span>由于系统正在升级阶段,现暂停部分积分赠送功能(评论,点赞,分享,签到),该部分操作系统将不赠送积分.其他积分规则仍保留.</p>
 								</div>
 
							    
							    <span style="display: inline-block;">
	                                <button class="btn btn-success" id="focusAllQuestion">全选</button>　
	                                <button class="btn btn-warning" id="moveAllQuestion">取消全选</button>　
	                                <button class="btn btn-info" id="fanQuestion">反选</button>　
	                                <button class="btn btn-danger" id="delQuestionByIds">删除</button>
	                                <button class="btn btn-warning" id="noAnswer">未回答</button>
	                                <button class="btn btn-success" id="yesAnswer">已回答</button>
                               	</span>
                               
                            <table class="table">
                                <thead>
                                <tr>
                                    <th style="width: 10%;">问题编号</th>
                                    <th style="width: 10%;">用户ID</th>
                                    <th style="width: 60%;">标题</th>
                                    <th style="width: 10%;">提问时间</th>
                                    <th style="width: 10%;">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php
                                foreach ($questions as $question) {
                                    print <<<table
        <tr>
     	  <td><input type="checkbox"  name="question_ids" value="{$question['id']}" />{$question['id']}</td>
		  <td>{$question['uid']}</td>
		  <td>{$question['question']}</td>
		  <td>{$question['time']}</td>
		  <td>
			<a class="btn btn-primary" href="edit_question.php?id={$question['id']}" target="_blank" title="编辑"><i class="icon-edit"></i></a>
		  </td>
        </tr>
table;
                                } ?>
                                </tbody>
                             </table>
                            
                             <span style="text-align: right;display: inline-block;float: right;">
                                    <a class="btn btn-success" href="?page_num=<?= $page_num - 1; ?>&question_name=<?=$question_name?>&type=<?=$_GET['type']?>">上一页</a>　
                                    <a class="btn btn-warning" href="?page_num=<?= $page_num + 1 ?>&question_name=<?=$question_name?>&type=<?=$_GET['type']?>">下一页</a>　
                                        　	 	 </span>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="profile">
                    </div>
                </div>

            </div>

        </div>
    </div>
</div>

<script src="lib/bootstrap/js/bootstrap.js"></script>
</body>
</html>